package com.web.cms.doc.provider;

import com.blue.core.dict.UploadType;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.web.cms.doc.exception.DocFileException;
import com.web.cms.doc.facade.DocFileFacade;
import com.web.cms.doc.message.DocFileRequest;
import com.web.cms.doc.message.DocFileResponse;
import com.web.cms.doc.model.DocFile;
import com.web.cms.doc.service.DocFileService;
import com.web.cms.doc.util.DocFileConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("cmsDocFileProvider")
public class DocFileProvider implements DocFileFacade
{
	@Autowired
	private DocFileService docFileService;
	@Autowired
	private StorageClient storageClient;
	
	public DocFileProvider()
	{
	}

	@Override
	public DocFileResponse add(DocFileRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		DocFile file = DocFileConvertor.toDocFile(request);
		file = docFileService.add(file);
		DocFileResponse response = DocFileConvertor.toResponse(file, storageClient);
		return response;
	}

	@Override
	public List<DocFileResponse> listByDoc(Integer docId, UploadType type)
	{
		if (docId == null)
			throw new DocFileException("文档ID不能为空");
		
		DocFile file = new DocFile();
		file.setDocId(docId);
		file.setType(type);
		List<DocFile> list = docFileService.list(file);
		List<DocFileResponse> responseList = DocFileConvertor.toResponse(list, storageClient);
		return responseList;
	}

	@Override
	public Page<DocFileResponse> listPage(DocFileRequest request, int start, int size)
	{
		DocFile file = DocFileConvertor.toDocFile(request);
		Page<DocFile> page = new Page<>(size);
		page.setCurPage(start);
		page = docFileService.list(file, page);
		Page<DocFileResponse> responsePage = DocFileConvertor.toResponse(page, storageClient);
		return responsePage;
	}
	
}
